package com.glomozda.machinerepairdao;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glomozda.machinerepairdao.domain.client.Client;
import com.glomozda.machinerepairdao.domain.machine.Machine;
import com.glomozda.machinerepairdao.domain.order.Order;
import com.glomozda.machinerepairdao.domain.repairtype.RepairType;
import com.glomozda.machinerepairdao.domain.user.User;
import com.glomozda.machinerepairdao.repository.client.ClientRepository;
import com.glomozda.machinerepairdao.repository.machine.MachineRepository;
import com.glomozda.machinerepairdao.repository.order.OrderRepository;
import com.glomozda.machinerepairdao.repository.repairtype.RepairTypeRepository;
import com.glomozda.machinerepairdao.repository.user.UserRepository;

/**
 * Handles requests for the application home page.
 */
@SuppressWarnings({"PMD.CommentRequired", "PMD.LawOfDemeter"})
@Controller
public class HomeController {
	
	private static final Logger lOGGER = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(final Locale locale, final Model model) {
		lOGGER.info("Welcome home! The client locale is {}.", locale);
		
		final Date date = new Date();
		final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		final String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		final ApplicationContext appCtx = 
                new ClassPathXmlApplicationContext("persistenceContext.xml");
		
		final MachineRepository mrepository = appCtx.getBean("machineRepository", MachineRepository.class);
        final List<Machine> machines = mrepository.findAll();
        String allMachines = new String();
        for (final Machine machine : machines) {
            allMachines = allMachines.concat(machine.toString());
        }
		
        model.addAttribute("allMachines", allMachines);
        
        final RepairTypeRepository rtrepository = appCtx.getBean("repairTypeRepository", RepairTypeRepository.class);
        final List<RepairType> repairTypes = rtrepository.findAll();
        String allRepairTypes = new String();
        for (final RepairType repairType : repairTypes) {
            allRepairTypes = allRepairTypes.concat(repairType.toString());
        }
		
        model.addAttribute("allRepairTypes", allRepairTypes);
        
        final ClientRepository clrepository = appCtx.getBean("clientRepository", ClientRepository.class);
        final List<Client> clients = clrepository.findAll();
        String allClients = new String();
        for (final Client client : clients) {
            allClients = allClients.concat(client.toString());
        }
		
        model.addAttribute("allClients", allClients);
        
        final UserRepository urepository = appCtx.getBean("userRepository", UserRepository.class);
        final List<User> users = urepository.findAll();
        String allUsers = new String();
        for (final User user : users) {
            allUsers = allUsers.concat(user.toString());
        }
		
        model.addAttribute("allUsers", allUsers);
        
        final OrderRepository orepository = appCtx.getBean("orderRepository", OrderRepository.class);
        final List<Order> orders = orepository.findAll();
        String allOrders = new String();
        for (final Order order : orders) {
            allOrders = allOrders.concat(order.toString());
        }
		
        model.addAttribute("allOrders", allOrders);
        
        ((AbstractApplicationContext)appCtx).close();
        
        return "home";
	}
	
}
